import 'package:flutter/material.dart';
import 'package:flutter_company_app/model/product.dart';
import 'package:flutter_company_app/services/product.dart';

import 'product/product_list_page.dart';

class ProductPage extends StatefulWidget {
  const ProductPage({Key key}) : super(key: key);

  @override
  _ProductPageState createState() => _ProductPageState();
}

class _ProductPageState extends State<ProductPage> {
  ProductListModel listData = ProductListModel.fromJson([]);
  int page = 0;

  void getProductList() async {
    var data = await getProductResult(page++);
    ProductListModel list = ProductListModel.fromJson(data);
    setState(() {
      // 一次性将所获取的内容全部填充listData中
      listData.data.addAll(list.data);
    });
  }

  @override
  void initState() {
    super.initState();
    getProductList();
  }

  @override
  Widget build(BuildContext context) {
    return ProductListPage(list: listData, getNextPage: () => getProductList());
  }
}
